import {getSchoolList,getMemberList} from '../../api/member'
import {editInfo,getUserInfoDetail} from '../../api/login'

Component({
  data:{
    title:"注册信息",
    sexList:[{
      name:"男",
      value:"0"
    },{
      name:"女",
      value:"1"
    }],
    schoolList:[],
    show:false,
    formData:{
      nickName:"",
      userName:"",
      sex:"",
      idcard:"",
      birthday:"",
      phonenumber:"",
      address:"",
      inviteCode:"",
      schoolId:""
    },
    schoolText:"",
        // 正则规则
        rule:[{
            props:"nickName",
            ruleVal:[{
              type:"requierd",
              tooltip:"昵称不能为空",
            }]
          },
          {
            props:"userName",
            ruleVal:[{
              type:"requierd",
              tooltip:"姓名不能为空",
            }]
          },
          {
            props:"sex",
            ruleVal:[{
              type:"requierd",
              tooltip:"请选择性别",
            }]
          },{
            props:"idcard",
            ruleVal:[{
              type:"requierd",
              tooltip:"身份证号不能为空",
            },{
            type:"idCard",
            tooltip:"身份证号格式不正确"
            }
          ]
          },
          {
            props:"birthday",
            ruleVal:[{
              type:"requierd",
              tooltip:"请选择出生日期",
            }]
          },
          {
            props:"phonenumber",
            ruleVal:[{
              type:"requierd",
              tooltip:"联系方式不能为空",
            },{
            type:"phone",
            tooltip:"联系方式格式不正确",
            }
          ]
          },
          {
            props:"address",
            ruleVal:[{
              type:"requierd",
              tooltip:"请填写家庭地址",
            }]
          }
        ]
  },
  methods:{
    onLoad(data:any){
        this.getUserInfo()
      if(data.userInfoObj){
        const res =  JSON.parse(decodeURIComponent(data.userInfoObj))
        const formData = this.data.formData as any
        for(let key in formData){
          formData[key] = res[key]
        }
  
        this.setData({
          title:"修改用户信息",
          formData,
          schoolText:this.getSchoolText(formData.schoolId)
        })
      }
      this.getAreaList()

    },
    getSchoolText(id:any){
      const schoolList = getApp().getSchoolList()
     const text = schoolList.filter((val:any)=>{
        return val.id == id
      })[0]?.name || ""
      return text
    },
    showPopup(){
      this.setData({ show: true });
    },
    onClose() {
      this.setData({ show: false });
    },
    // 改变Form表单
    handelChange(e:any){
    const {props,value,id} = e.detail 
    let property:any = this.data.formData
    if(["sex","schoolId"].includes(props)){
      property[props] = id
    }else{
      property[props] = value
    }
    this.setData({
      formData:property,
    })

    },
        // 提交前校验
        frontCheck(){
          const childComponents = this.selectAllComponents('.form-vant')
          if(childComponents.length > 0){
            const ruleList = this.data.rule as any
            ruleList.forEach((item:any,index:number) => {
              childComponents[index].commonRules(item.props)
            });
           const isPass = childComponents.some((item:any)=>{
            return item.data.isAllPass === false
            })
            return isPass
          }
        },
    // 确认提交
   async confirm(){
     try {
              //  规则校验
    const isNoPass = this.frontCheck()
    if(isNoPass) return
      const postData = this.data.formData as any
      postData.userId = getApp().getUserInfo().userId
      const {code} = await editInfo(postData)
      if(code === 200){
        wx.Utils.toast("提交成功!")
        // 获取用户信息
        this.getUserInfo(postData.userId);
        //获取名下成员列表
        const {data:memberList} =  await getMemberList({userId:postData.userId})
        getApp().setMemberList(memberList)
        if(this.data.title === "修改用户信息"){
        wx.navigateBack()
        }else{
          wx.switchTab({
            url:"/pages/index/index"
          })
        }
      }else{
        wx.Utils.toast('提交失败')
      }  
     } catch (error) {
       return Promise.reject(error)
     }
    },
    // 获取用户信息
    async getUserInfo(userId?:any){
      const {data:userInfo} = userId ? await getUserInfoDetail({
        userId 
      }) : await getUserInfoDetail();    
      getApp().setUserInfo('userInfo',userInfo)
    },
    // 取消提交
    cancel(){
    wx.navigateBack()
    },
    // 获取地区列表
   async getAreaList(){
      const {data:list} =  await getSchoolList()
      const schoolList = list.map((val:{name:string,id:string | number})=>{
        return {
          name:val.name,
          value:val.id
        }
      })
      const saveSchoolList = schoolList.map((val:any)=>{
        return {
          id:val.value,
          name:val.name
        }
      })
      getApp().setSchoolList(saveSchoolList)
      this.setData({
        schoolList,
      })
    },

  }
})